package com.yike.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.product.entity.GoodsBrand;
import com.yike.product.mapper.GoodsBrandMapper;
import com.yike.product.param.GoodsBrandParam;
import com.yike.product.service.GoodsBrandService;
import com.yike.product.param.GoodsBrandPageParam;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.product.vo.GoodsBrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 商品品牌 服务实现类
 *
 * @author Sakura
 * @since 2024-10-18
 */
@Slf4j
@Service
public class GoodsBrandServiceImpl extends BaseServiceImpl<GoodsBrandMapper, GoodsBrand> implements GoodsBrandService {

    @Autowired
    private GoodsBrandMapper goodsBrandMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveGoodsBrand(GoodsBrandParam goodsBrandParam) throws Exception {
        // 先验证是否有同名品牌
        GoodsBrand goodsBrand = goodsBrandMapper.selectOne(Wrappers.lambdaQuery(GoodsBrand.class)
                .eq(GoodsBrand::getName, goodsBrandParam.getName()));
        if (goodsBrand != null) {
            throw new BusinessException("当前品牌已存在，请勿重复添加");
        }

        // 保存品牌信息
        goodsBrand = new GoodsBrand();
        BeanUtils.copyProperties(goodsBrandParam, goodsBrand);
        goodsBrand.setCreateBy(LoginUtil.getUserId());
        return super.save(goodsBrand);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGoodsBrand(GoodsBrandParam goodsBrandParam) throws Exception {
        // 先判断信息是否存在
        GoodsBrand goodsBrand = goodsBrandMapper.selectById(goodsBrandParam.getId());
        if (goodsBrand == null) {
            throw new BusinessException("品牌信息异常");
        }
        // 判断修改的名称是否和已有的重复
        Integer checkNum = goodsBrandMapper.selectCount(Wrappers.lambdaQuery(GoodsBrand.class)
                .eq(GoodsBrand::getName, goodsBrandParam.getName())
                .ne(GoodsBrand::getId, goodsBrandParam.getId()));
        if (checkNum > 0) {
            throw new BusinessException("当前品牌已存在，请勿重复添加");
        }
        BeanUtils.copyProperties(goodsBrandParam, goodsBrand);
        goodsBrand.setUpdateBy(LoginUtil.getUserId());
        return super.updateById(goodsBrand);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteGoodsBrand(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public GoodsBrandVo getGoodsBrand(Long id) throws Exception {
        return goodsBrandMapper.getGoodsBrandVo(id);
    }

    @Override
    public Paging<GoodsBrandVo> getGoodsBrandPageList(GoodsBrandPageParam goodsBrandPageParam) throws Exception {
        Page<GoodsBrand> page = new PageInfo<>(goodsBrandPageParam);
        IPage<GoodsBrandVo> iPage = goodsBrandMapper.getGoodsBrandList(page, goodsBrandPageParam);
        return new Paging<GoodsBrandVo>(iPage);
    }

}
